#include "HelpLayer.h"
#include "AudioManage.h"

#define PNG_HELP_BASE "Help/help_base.png"
#define PNG_HELP_1 "Help/help_1.png"
#define PNG_HELP_2 "Help/help_2.png"
#define PNG_HELP_3 "Help/help_3.png"
#define PNG_HELP_4 "Help/help_4.png"
#define PNG_HELP_5 "Help/help_5.png"
#define PNG_HELP_6 "Help/help_6.png"
#define PNG_HELP_7 "Help/help_7.png"
#define PNG_NEXT "Help/arrow_right.png"
#define PNG_LAST "Help/arrow_left.png"
#define PNG_NEXT_DISABLE "Help/arrow_right_disable.png"
#define PNG_LAST_DISABLE "Help/arrow_left_disable.png"
#define PNG_EXIT_HELP "Help/help_exit.png"
#define PNG_POINT_W "Help/notselected_page.png"
#define PNG_POINT_B "Help/selected_page.png"

#define TIME_FADE_OUT  0.3f
#define TIME_FADE_IN  0.3f

HelpLayer::HelpLayer()
{
}

HelpLayer::~HelpLayer()
{
}

bool HelpLayer::init()
{
	Layer::init();

	LayerColor* layerColor = LayerColor::create(cocos2d::Color4B(225, 225, 225, 100));
	Size size = Director::getInstance()->getVisibleSize();
	this->addChild(layerColor);

	Sprite* bg = Sprite::create(PNG_HELP_BASE);
	bg->setPosition(Vec2(size.width / 2, size.height / 2));
	this->addChild(bg);


	std::string helpImages[7] = { PNG_HELP_1, PNG_HELP_2, PNG_HELP_3,
		PNG_HELP_4, PNG_HELP_5, PNG_HELP_6, PNG_HELP_7 };

	for (int i = 0; i < 7; i++)
	{
		this->helps[i] = Sprite::create(helpImages[i]);
		helps[i]->setPosition(Vec2(size.width / 2, size.height / 2));
		this->addChild(helps[i]);
		helps[i]->setVisible(false);
	}

	helps[0]->setVisible(true);

	m_bg = bg;
	m_Index = 0;

	nextItem = MenuItemImage::create(PNG_NEXT, PNG_NEXT, CC_CALLBACK_0(HelpLayer::nextStep, this));
	nextItem->setScale(0.9f);
	nextItem->setPosition(Vec2(size.width * 0.8f, size.height * 0.25f));

	lastItem = MenuItemImage::create(PNG_LAST_DISABLE, PNG_LAST, CC_CALLBACK_0(HelpLayer::lastStep, this));
	lastItem->setScale(0.9f);
	lastItem->setPosition(Vec2(size.width * 0.2, size.height * 0.25f));

	MenuItemImage* exitItem = MenuItemImage::create(PNG_EXIT_HELP, PNG_EXIT_HELP, CC_CALLBACK_0(HelpLayer::exitHelp, this));
	exitItem->setScale(0.9f);
	exitItem->setPosition(Vec2(size.width * 0.82f, size.height * 0.72f));
	
	Menu* helpMenu = Menu::create(nextItem, lastItem, exitItem, NULL);
	helpMenu->setPosition(Vec2::ZERO);
	this->addChild(helpMenu, 11);

	for (int i = 0; i < 7; i++)
	{
		Sprite* p = Sprite::create(PNG_POINT_W);
		this->pPosition[i] = Vec2(size.width * (0.32f + 0.06f * i) , size.height * 0.25f);
		p->setPosition(pPosition[i]);
		this->addChild(p, 1);
	}

	this->pointBlue = Sprite::create(PNG_POINT_B);
	pointBlue->setPosition(pPosition[0]);
	this->addChild(pointBlue, 2);

	auto listener1 = EventListenerTouchOneByOne::create();
	listener1->setSwallowTouches(true);
	listener1->onTouchBegan = CC_CALLBACK_0(HelpLayer::callBack, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
	m_listener = listener1;

	return true;
}

void HelpLayer::nextStep()
{
	AudioManage::buttonSound();

	Sprite* l = Sprite::create(PNG_LAST);
	this->lastItem->setNormalImage(l);

	if (6 == m_Index)
	{
		return;
	}
	else if (5 == m_Index)
	{
		Sprite* nD = Sprite::create(PNG_NEXT_DISABLE);
		this->nextItem->setNormalImage(nD);
	}
	else
	{
		Sprite* n = Sprite::create(PNG_NEXT);
		this->nextItem->setNormalImage(n);
	}

	
	helps[m_Index]->runAction(Sequence::create(
		FadeOut::create(TIME_FADE_OUT),
		Hide::create(),
		NULL
		));
	
	m_Index++;

	this->pointBlue->setPosition(this->pPosition[m_Index]);
	helps[m_Index]->runAction(Sequence::create(
		DelayTime::create(TIME_FADE_OUT),
		FadeOut::create(0),
		Show::create(),
		FadeIn::create(TIME_FADE_IN),
		NULL
		));
}

void HelpLayer::lastStep()
{
	AudioManage::buttonSound();

	Sprite* n = Sprite::create(PNG_NEXT);
	this->nextItem->setNormalImage(n);

	if (0 == m_Index)
	{
		return;
	}
	else if (1 == m_Index)
	{
		Sprite* lD = Sprite::create(PNG_LAST_DISABLE);
		this->lastItem->setNormalImage(lD);
	}
	else
	{
		Sprite* l = Sprite::create(PNG_LAST);
		this->lastItem->setNormalImage(l);
	}


	helps[m_Index]->runAction(Sequence::create(
		FadeOut::create(TIME_FADE_OUT),
		Hide::create(),
		NULL
		));

	m_Index--;

	this->pointBlue->setPosition(this->pPosition[m_Index]);
	helps[m_Index]->runAction(Sequence::create(
		DelayTime::create(TIME_FADE_OUT),
		FadeOut::create(0),
		Show::create(),
		FadeIn::create(TIME_FADE_IN),
		NULL
		));
}

void HelpLayer::exitHelp()
{
	AudioManage::button2Sound();

	this->setVisible(false);
	_eventDispatcher->removeEventListener(m_listener);
}

bool HelpLayer::callBack()
{
	return true;
}

void HelpLayer::backToFirstPage()
{
	m_Index = 0;

	for (int i = 1; i < 7; i++)
	{
		helps[i]->setVisible(false);
	}
	helps[0]->runAction(
		Sequence::create(
		FadeIn::create(0),
		Show::create(),
		NULL));

	pointBlue->setPosition(this->pPosition[0]);

	Sprite* lD = Sprite::create(PNG_LAST_DISABLE);
	this->lastItem->setNormalImage(lD);

	auto listener1 = EventListenerTouchOneByOne::create();
	listener1->setSwallowTouches(true);
	listener1->onTouchBegan = CC_CALLBACK_0(HelpLayer::callBack, this);

	_eventDispatcher->addEventListenerWithSceneGraphPriority(listener1, this);
	m_listener = listener1;
}